package com.snapan.controller;

import com.snapan.common.PageResult;
import com.snapan.entity.UserFile;
import com.snapan.service.RecycleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 回收站控制器
 */
@RestController
@RequestMapping("/api/recycle")
public class RecycleController {

        @Autowired
        private RecycleService recycleService;

        /**
         * 获取回收站文件列表
         */
        @GetMapping("/list")
        //TODO: 2025-09-04 userId应该从session获取，而不是前端传值，保证安全。
        public Map<String, Object> getList(@RequestParam int pageSize,
                                           @RequestParam int pageNo,
                                           @RequestParam int userId,
                                           @RequestParam int parentId) {
            Map<String, Object> result = new HashMap<>();
            try {
                PageResult<UserFile> page = recycleService.selectByPage(pageSize, pageNo, userId, parentId);
                result.put("success", true);
                result.put("data", page);
                result.put("message", "获取文件列表成功");
            } catch (Exception e) {
                result.put("success", false);
                result.put("message", "获取文件列表失败：" + e.getMessage());
            }
            return result;
        }
        /**
         * 还原回收站文件
         */
        @PostMapping("/restore/{id}")
        public Map<String, Object> restore(@PathVariable Long id){
            Map<String, Object> result = new HashMap<>();
            try {
                Integer ans = recycleService.restore(id);
                result.put("success", true);
                result.put("data", ans);
                result.put("message", "还原文件成功");
            } catch (Exception e) {
                result.put("success", false);
                result.put("message", "还原文件失败：" + e.getMessage());
            }
            return result;
        }

        /**
         * 彻底删除回收站文件
         */
        @DeleteMapping("/{id}")
        public Map<String, Object> permanentDelete(@PathVariable Long id){
            Map<String, Object> result = new HashMap<>();
            try {
                Integer ans = recycleService.permanentDelete(id);
                result.put("success", true);
                result.put("data", ans);
                result.put("message", "彻底删除成功");
            } catch (Exception e) {
                result.put("success", false);
                result.put("message", "彻底删除失败：" + e.getMessage());
            }
            return result;
        }
}
